package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.db.conn;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;



public class orders_List extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public orders_List() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	//根据条件返回订单信息
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html;");
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setHeader("Access-Control-Allow-Methods", "GET,POST");
		PrintWriter out = response.getWriter();
		JSONArray jsonarray = new JSONArray();
		JSONObject jsonobj = new JSONObject();


		conn cn = new conn();
	
	    String f=request.getParameter("f");

		ResultSet rs=null;

		if(f.equals("1"))
		{
			String t = request.getParameter("t");
			String uid = request.getParameter("uid");

			rs=cn.query("select * from  orders where  flag="+t+" and uid="+uid+" order by no desc ");
		}
		else if(f.equals("2"))
		{
			String id = request.getParameter("id");
			rs=cn.query("select * from  orders where no='"+id+"'  ");
		}

		while (true)
		{
			try {
				if (!rs.next()) break;
             jsonobj.put("no", rs.getString("no"));
             jsonobj.put("uid", rs.getString("uid"));
             jsonobj.put("username", rs.getString("username"));
             jsonobj.put("phone", rs.getString("phone"));
             jsonobj.put("address", rs.getString("address"));
             jsonobj.put("ormemo", rs.getString("ormemo"));
             jsonobj.put("smoney", rs.getString("smoney"));
             jsonobj.put("oflag", rs.getString("oflag"));
             jsonobj.put("otime", rs.getString("otime"));
             jsonobj.put("flag", rs.getString("flag"));

				if ( cn.exist("select * from reimburse where no='"+rs.getString("no")+"'")) {
					jsonobj.put("flag2", "1");
				} else {
					jsonobj.put("flag2", "0");
				}

             if (f.equals("2"))
			 {
				 ResultSet rs2 = null;

				 if ( rs.getString("flag").equals("0")) {
					rs2= cn.query("select * from ordermore a " +
							 "  left join products p on a.pid = p.pid  " +
							 " where a.no='" + rs.getString("no")+"'");
				 } else {
					 rs2= cn.query("select * from ordermore a " +
							 "  left join dproducts p on a.pid = p.pid  " +
							 " where a.no='" + rs.getString("no")+"'");
				 }
				 JSONArray jsonArray2 = new JSONArray();
				 JSONObject jsonobj2 = new JSONObject();
				 while (rs2.next()) {
					 jsonobj2.put("id", rs2.getString("id"));
					 jsonobj2.put("ptitle", rs2.getString("ptitle"));
					 jsonobj2.put("photo", rs2.getString("photo"));
					 jsonobj2.put("quantity", rs2.getString("quantity"));
					 jsonobj2.put("price", rs2.getString("price"));
					 jsonArray2.add(jsonobj2);
				 }

				 jsonobj.element("more",jsonArray2);


				 ResultSet rs3 = cn.query("select * from  orderstatus " +
						 " where no='" + rs.getString("no")+"' order by  id desc ");
				 JSONArray jsonArray3 = new JSONArray();
				 JSONObject jsonobj3 = new JSONObject();
				 while (rs3.next()) {
					 jsonobj3.put("id", rs3.getString("id"));
					 jsonobj3.put("omemo", rs3.getString("omemo"));
					 jsonobj3.put("otime", rs3.getString("otime"));

					 jsonArray3.add(jsonobj3);
				 }

				 jsonobj.element("jd",jsonArray3);
			 }
		
				jsonarray.add(jsonobj);

			} catch (SQLException e) {
				e.printStackTrace();
			}

		}

		out.println(jsonarray);	
		out.flush();
		out.close();
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}

